package com.htht.smartschool.action.card;

import java.util.List;
import java.util.Map;

import com.google.gson.reflect.TypeToken;
import com.htht.smartschool.action.baseaction.BaseAction;
import com.htht.smartschool.common.model.ParamData;
import com.htht.smartschool.common.servlet.DataContainer;
import com.htht.smartschool.model.amq.Mstatus;
import com.htht.smartschool.model.card.Rings;
import com.htht.smartschool.model.card.TBdworkhour;
import com.htht.smartschool.model.card.TCard;
import com.htht.smartschool.model.card.TFamilyphone;
import com.htht.smartschool.model.card.TPhonebook;
import com.htht.smartschool.model.card.TPhonebooktemp;
import com.htht.smartschool.model.card.TSosphone;
import com.htht.smartschool.service.card.CardService;
import com.htht.smartschool.util.cmpp.CmppData;
import com.htht.smartschool.util.system.GsonUtils;

public class CardAction extends BaseAction {

	@SuppressWarnings("rawtypes")
	@Override
	public boolean processData(DataContainer obj, ParamData pd) {
		CardService  cardService = new CardService();
		boolean flag = false;
		TCard card = null;
		TPhonebook book = null;
		try {
			book = (TPhonebook) GsonUtils.getInstance().fromJson(pd.getData(), TPhonebook.class);
			card = (TCard) GsonUtils.getInstance().fromJson(pd.getData(), TCard.class);
		} catch (Exception e) {
		}
		if(pd.getCmd().equalsIgnoreCase("addCard")){
			card = cardService.addCard(card);
			if(card != null){
				flag = true;
				obj.setData(card);
			}else{
				obj.setData("添加终端失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("deleteCard")){
			boolean result = cardService.deleteCard(card);
			if(result){
				flag = true;
				obj.setData(result);
			}else{
				obj.setData("删除终端失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("updateCard")){
			boolean result = cardService.updateCard(card);
			if(result){
				flag = true;
				obj.setData(result);
			}else{
				obj.setData("修改终端失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("getCard")){
			List<TCard> list = cardService.getCard(card);
			if(list.size()>0){
				flag = true;
				obj.setData(list);
			}else{
				obj.setData("查无数据！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("getCards")){
			Map map = cardService.getCards(card);
			if(map != null){
				flag = true;
				obj.setData(map);
			}else{
				obj.setData("查无数据！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("addPhonebooks")){
			boolean result = cardService.addPhonebooks(book);
			if(result){
				flag = true;
				obj.setData(result);
			}else{
				obj.setData("白名单已满50请修改！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("updatePhonebook")){
			boolean result = cardService.updatePhonebook(book);
			if(result){
				flag = true;
				obj.setData(result);
			}else{
				obj.setData("白名单已满50请修改！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("getPhonebooks")){
			List<TPhonebook> books = cardService.getPhonebooks(card);
			if(books.size() >0){
				flag = true;
				obj.setData(books);
			}else{
				obj.setData("查无数据！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("deletePhonebooks")){
			List<TPhonebook> books = GsonUtils.getInstance().gson.fromJson(pd.getData(),  new TypeToken<List<TPhonebook>>(){}.getType());
			boolean result = cardService.deletePhonebooks(books);
			if(result){
				flag = true;
				obj.setData(result);
			}else{
				obj.setData("删除白名单失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("addPhonebookTmps")){
			List<TPhonebooktemp> booktemps = GsonUtils.getInstance().gson.fromJson(pd.getData(),  new TypeToken<List<TPhonebooktemp>>(){}.getType());
			boolean result = cardService.addPhonebookTmps(booktemps);
			if(result){
				flag = true;
				obj.setData(result);
			}else{
				obj.setData("添加临时白名单失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("deletePhonebookTmps")){
			List<TPhonebooktemp> books = GsonUtils.getInstance().gson.fromJson(pd.getData(),  new TypeToken<List<TPhonebooktemp>>(){}.getType());
			boolean result = cardService.deletePhonebookTmps(books);
			if(result){
				flag = true;
				obj.setData(result);
			}else{
				obj.setData("删除临时白名单失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("addFamilyphones")){
			List<TFamilyphone> phones = GsonUtils.getInstance().gson.fromJson(pd.getData(),  new TypeToken<List<TFamilyphone>>(){}.getType());
			boolean result = cardService.addFamilyphones(phones);
			if(result){
				flag = true;
				obj.setData(result);
			}else{
				obj.setData("添加亲情号码失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("getFamilyphones")){
			List<TFamilyphone> phones = cardService.getFamilyphones(card);
			if(phones.size() > 0){
				flag = true;
				obj.setData(phones);
			}else{
				obj.setData("查无数据！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("deleteFamilyphones")){
			List<TFamilyphone> phones = GsonUtils.getInstance().gson.fromJson(pd.getData(),  new TypeToken<List<TFamilyphone>>(){}.getType());
			boolean result = cardService.deleteFamilyphones(phones);
			if(result){
				flag = true;
				obj.setData(result);
			}else{
				obj.setData("删除亲情号码失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("addSosphones")){
			List<TSosphone> soss = GsonUtils.getInstance().gson.fromJson(pd.getData(),  new TypeToken<List<TSosphone>>(){}.getType());
			boolean result = cardService.addSosphones(soss);
			if(result){
				flag = true;
				obj.setData(result);
			}else{
				obj.setData("添加sos号码失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("getSosphones")){
			List<TSosphone> soss = cardService.getSosphones(card);
			if(soss.size() >0){
				flag = true;
				obj.setData(soss);
			}else{
				obj.setData("查无数据！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("deleteSosphones")){
			List<TSosphone> soss = GsonUtils.getInstance().gson.fromJson(pd.getData(),  new TypeToken<List<TSosphone>>(){}.getType());
			boolean result = cardService.deleteSosphones(soss);
			if(result){
				flag = true;
				obj.setData(result);
			}else{
				obj.setData("删除sos号码失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("sendTextToCard")){
			boolean result = cardService.sendTextToCard(card);
			if(result){
				flag = true;
				obj.setData(result);
			}else{
				obj.setData("给终端留言失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("setCardWork")){
			TBdworkhour work = (TBdworkhour) GsonUtils.getInstance().fromJson(pd.getData(), TBdworkhour.class);
			boolean result = cardService.setCardWork(work);
			if(result){
				flag = true;
				obj.setData(result);
			}else{
				obj.setData("设置终端工作时间失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("getCardWork")){
			TBdworkhour work = (TBdworkhour) GsonUtils.getInstance().fromJson(pd.getData(), TBdworkhour.class);
			TBdworkhour result = cardService.getCardWork(work);
			if(result != null){
				flag = true;
				obj.setData(result);
			}else{
				obj.setData("查询失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("getAllRings")){
			List<Rings> result = cardService.getAllRings();
			if(result.size() > 0){
				flag = true;
				obj.setData(result);
			}else{
				obj.setData("查无数据！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("sendWarnType")){
			Mstatus mstatus  =  (Mstatus) GsonUtils.getInstance().fromJson(pd.getData(), Mstatus.class);
			boolean result = cardService.sendWarnType(mstatus);
			if(result){
				flag = true;
				obj.setData(result);
			}else{
				obj.setData("发送失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("setGPS")){
			boolean result = cardService.setGPS(card.getImei(),card.getGpsstatus());
			if(result){
				flag = true;
				obj.setData(result);
			}else{
				obj.setData("发送失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("sendCmpp")){
			CmppData cd = (CmppData) GsonUtils.getInstance().fromJson(pd.getData(), CmppData.class);
			boolean result = cardService.sendCmpp(cd);
			if(result){
				flag = true;
				obj.setData(result);
			}else{
				obj.setData("发送失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("queryYE_old")){
			String result = cardService.queryYE_old(card.getImei());
			if(result != null && result.length() > 0){
				flag = true;
				obj.setData(result);
			}else{
				obj.setData("查询失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("queryYE")){
			boolean result = cardService.queryYE(card.getImei());
			if(result){
				flag = true;
				obj.setData(result);
			}else{
				obj.setData("发送失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("deleteWhiteListInfo")){//删除白名单信息
			boolean result = cardService.deleteWhiteListInfo(book);
			if(result){
				flag = true;
				obj.setData(result);
			}else{
				obj.setData("删除失败！");
			}
		}
		return flag;
	}

}
